<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<style>
.divTest {
	border: 1px solid red;
}

th {
	text-align: center;
	font-size: 10px;
}

td {
	text-align: center;
	font-size: 8px;
}

.divBanner {
	height: 300px;
}

.divBannerImgs {
	height: 90%;
	top: 5px;
	overflow: hidden;
}

.divBannerTable {
	height: 90%;
	top: 5px;
	border: 1px solid #e7e7e7;
	overflow: scroll;
	overflow-X: hidden;
}

.divCompanyInfo {
	margin-top: 30px;
	height: 220px;
}

.divCompanyPartners {
	height: 400px;
}

.btm {
	border-radius: 5px 5px 5px 5px;
	outline: none;
}

.inputFile {
	background-color: transparent;
	font-size: 15px
}

#dataOne {
	border: 1px solid #e7e7e7;
	border-radius: 15px 15px 15px;
	height: 85%
}
.companyDemo{
	height: 100%
}
.companyTable{
	height: 100%;
	border: 1px solid #e7e7e7;
	overflow: scroll;
	overflow-X: hidden;
}
</style>
<div class="divBanner col-sm-12">
	<div class="subTitle" style="margin-top: 15px">
		<div class="horizontal-line"></div>
		<div class="title-text">首页Banner</div>
	</div>
	<div class="col-sm-12">
		<button id="upload" style="float: right;"
			class="btn btn-sm btn-success">上 传</button>
		<input style="float: right;" id="bannerFile" class="inputFile"
			type="file" name="file" id="fileOne" />
	</div>
	<div class="divBannerImgs col-sm-7">
		<img id="bannerImg" style="height: 100%; width: 100%; display: none"
			alt="" src="" />
	</div>
	<div class=" divBannerTable col-sm-5">
		<table style="height: 100%" class="table table-condensed table-hover table-bordered">
			<thead>
				<tr>
					<th style="display: none">ID</th>
					<th>图片标题</th>
					<th style="display: none">图片路径</th>
					<th>状态</th>
					<th>排序</th>
					<th>操作</th>
				</tr>
			</thead>
			<tbody id="bannerTableBody">
			</tbody>
		</table>
	</div>
</div>
<div class="divCompanyInfo col-sm-12">
	<div class="subTitle" style="margin-top: 20px">
		<div class="horizontal-line"></div>
		<div class="title-text">数据信息</div>
	</div>
	<div id="dataOne" class="col-sm-4">
		<form class="form-horizontal">
			<div class="form-group" style="margin-top: 10px">
				<label class="col-sm-3 control-label">标题：</label>
				<div class="col-sm-8">
					<input id="name1" style="text-align: center;" type="text" class="form-control" >
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">数据：</label>
				<div class="col-sm-8">
					<input id="data1" style="text-align: center;" type="text" class="form-control" >
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">介绍：</label>
				<div class="col-sm-8">
					<input id="text1" style="text-align: center;" type="text" class="form-control" >
				</div>
			</div>
		</form>
		<div style="text-align: center; margin-top: 5px" class="col-sm-12">
			<button data="1" onclick="save(this)" class="btn btn-primary">提交</button>
		</div>
	</div>

	<div id="dataOne" class="col-sm-4">
		<form class="form-horizontal">
			<div class="form-group" style="margin-top: 10px">
				<label class="col-sm-3 control-label">标题：</label>
				<div class="col-sm-8">
					<input id="name2" style="text-align: center;"type="text" class="form-control" >
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">数据：</label>
				<div class="col-sm-8">
					<input id="data2" style="text-align: center;" type="text" class="form-control">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">介绍：</label>
				<div class="col-sm-8">
					<input id="text2" style="text-align: center;" type="text" class="form-control">
				</div>
			</div>
		</form>
		<div style="text-align: center; margin-top: 5px" class="col-sm-12">
			<button data="2" onclick="save(this)" class="btn btn-primary">提交</button>
		</div>
	</div>

	<div id="dataOne" class="col-sm-4">
		<form class="form-horizontal">
			<div class="form-group" style="margin-top: 10px">
				<label class="col-sm-3 control-label">标题：</label>
				<div class="col-sm-8">
					<input id="name3" style="text-align: center;" type="text" class="form-control">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">数据：</label>
				<div class="col-sm-8">
					<input id="data3" style="text-align: center;" type="text" class="form-control">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">介绍：</label>
				<div class="col-sm-8">
					<input id="text3" style="text-align: center;" type="text" class="form-control">
				</div>
			</div>
		</form>
		<div style="text-align: center; margin-top: 5px" class="col-sm-12">
			<button data="3" onclick="save(this)" class="btn btn-primary">提交</button>
		</div>
	</div>

</div>
<div class="divCompanyPartners col-sm-12">
	<div class="subTitle" style="margin-top: 15px">
		<div class="horizontal-line"></div>
		<div class="title-text">合作企业</div>
	</div>
	<div class="companyDemo col-sm-6">
			<div class="col-sm-6">
				<div style="height: 300px;margin-top:30px;background-color: #e7eaec " class=" col-sm-12">
						<img id="showCompanyImg" style="height: 100%;width: 100%;border: 0px" />				
				</div>
			</div>
			<div class="col-sm-6">
				<div style="height: 100%;margin-top:30px " class=" col-sm-12">
					<div style="margin-top: 10px" class="form-group" >
						<label class="col-sm-4 control-label">企业名称：</label>
						<div class="col-sm-8">
							<input id="companyShowName" style="text-align: center;" type="text" class="form-control">
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-4 control-label">企业网址：</label>
						<div class="col-sm-8">
							<input id="companyUrl" style="text-align: center;" type="text" class="form-control">
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-4 control-label">所属模块：</label>
						<div class="col-sm-8">
							<select id="typeCode" style="text-align: center;" class="form-control">
									<option value="1">战略企业</option>
									<option value="2">合作案例</option>
							</select>
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-4 control-label">上传LOGO：</label>
						<div class="col-sm-8">
							<input  id="companyShowLogo" class="inputFile" type="file" name="file"/>
						</div>
					</div>
					<div class="form-group">
						<div class="col-sm-12">
							<textarea id="companyShowInfo" style="height: 200px;width: 100%"></textarea>
						</div>
					</div>
					<div class="form-group">
						<div class="col-sm-12">
							<button id="editCompanyInfoBtn" data-type="2" onclick="saveCompanyInfo(this)" class="btn btn-success">更新</button>
							<button id="saveCompanyInfoBtn" data-type="1" onclick="saveCompanyInfo(this)" class="btn btn-primary">保存</button>
						</div>
					</div>
				</div>
			</div>		
	</div>
	<div class="companyTable col-sm-6">
			<table style="height: 100%" class="table table-condensed table-hover table-bordered">
			<thead>
				<tr>
					<th>企业ID</th>
					<th>企业名称</th>
					<th>LOGO</th>
					<th>所属模块</th>
					<th>操作</th>
				</tr>
			</thead>
			<tbody id="companysTableBody">
				
			</tbody>
		</table>	
	</div>
</div>
<script type="text/javascript">
	/* 获取首页banner列表数据 */
	getBannerTableData();
	/* 上传首页banner */
	$('#upload').click(function(){
		$('#upload').attr('disabled',true);
		if (!document.getElementById("bannerFile").files[0]){
			toastr.error('请选择文件','失败');
			$('#upload').attr('disabled',false);
			return false;
		}
		var formData = new FormData();
		formData.append('file',document.getElementById("bannerFile").files[0]);
		formData.append('title',"BannerImg");
		formData.append('link',"");
		$.ajax({
			url:'<%=basePath%>/File/settingHeadImg/IndexBannerImg',
			type:'POST',
			data:formData,
			contentType: false,
			processData: false,
			success:function(data){
				if (data.code=='200'){
					toastr.success('上传成功','成功');
					setTimeout(function(){
						window.location.reload();
					}, 800);
				} else{
					toastr.error(data.message,'失败');
					$('#upload').attr('disabled',false);
				}
			},
			error:function(){
				toastr.error('网络异常','失败');
				$('#upload').attr('disabled',false);
			}
		});
	})
	/* 获取首页banner列表数据 */
	function getBannerTableData(){
		$.ajax({
			url:'<%=basePath%>Resource/getPicList/IndexBannerImg',
			type:'GET',
			dataType:'JSON',
			success:function(data){
				var html = '';
				if (data.code=='200'){
					$(data.data).each(function(index,ele){
						status = '停用';
						btnStatus = '启 用';
						if (ele.status==1){
							status = '启用中';
							btnStatus = '停 用';
						}
						html = html + '<tr><td>'+ele.title+'</td><td style="display: none">'+ele.path+'</td><td>'+status+'</td><td>'+ele.sort+'</td>'+
						'<td><button data="'+ele.path+'" onclick="showImg(this)" class="btm">查 看</button><button  data="'+ele.resourceId+'" data-type="up" onclick="changeSort(this)" class="btm">升序</button><button data-type="down"  data="'+ele.resourceId+'" onclick="changeSort(this)" class="btm">降序</button><button data="'+ele.resourceId+'" onclick="changeStatus(this)" class="btm">'+btnStatus+'</button><button data="'+ele.resourceId+'" onclick="delBannerImg(this)" class="btm">删 除</button></td></tr>'
					});
				}
				$('#bannerTableBody').html(html);
			},
			error:function(){
				toastr.error('网络异常加载数据失败','失败');
			}
		});
	}
	
	function changeStatus(obj){
		new $.flavr({
			content : '确认改变当前状态',
            dialog : 'confirm', 
            onConfirm : function(){ 
            	var id = $(obj).attr('data');
            	$.ajax({
        			url:'<%=basePath%>Resource/changeStatus',
        			type : 'POST',
        			dataType : 'JSON',
        			data : {
        				'resourceId' : id,
        			},
        			success : function(data) {
        				if (data.code == '200') {
        					toastr.success('提交成功', '操作成功');
        					getBannerTableData();
        				} else {
        					toastr.warning(data.message, '操作失败');
        				}
        			},
        			error : function() {
        				toastr.error('服务器发烧了>_<,请稍后再试', '网络异常');
        			}
        		});
            }, 
            onCancel : function(){
            	toastr.warning('取消操作','取消操作');
           	 } 
         });
	}
	
	function changeSort(obj){
		var type = $(obj).attr('data-type');
		var id = $(obj).attr('data');
		$.ajax({
			url:'<%=basePath%>Resource/changeSort',
			type : 'POST',
			dataType : 'JSON',
			data : {
				'resourceId' : id,
				'type':type
			},
			success : function(data) {
				if (data.code == '200') {
					toastr.success('成功', '操作成功');
					getBannerTableData();
				} else {
					toastr.warning(data.message, '操作失败');
				}
			},
			error : function() {
				toastr.error('服务器发烧了>_<,请稍后再试', '网络异常');
			}
		});
	}
	/* 展示图片 */
	function showImg(obj){
		$('#bannerImg').fadeOut(100);
		$('#bannerImg').attr('src',$(obj).attr('data'));
		$('#bannerImg').fadeIn(300);
	}
	/* 删除素材 */
	function delBannerImg(obj){
		var id = $(obj).attr('data');
		var tr = $(obj).parent().parent();
		new $.flavr({
			content : '确认删除该图片？',
            dialog : 'confirm', 
            onConfirm : function(){ 
            	$.ajax({
        			url:'<%=basePath%>Resource/del',
        			type : 'POST',
        			dataType : 'JSON',
        			data : {
        				'resourceId' : id,
        			},
        			success : function(data) {
        				if (data.code == '200') {
        					toastr.success('提交成功', '操作成功');
        					$(tr).fadeOut(200);
        				} else {
        					toastr.warning(data.message, '操作失败');
        				}
        			},
        			error : function() {
        				toastr.error('服务器发烧了>_<,请稍后再试', '网络异常');
        			}
        		});
            }, 
            onCancel : function(){
            	toastr.warning('取消操作','取消操作');
           	 } 
         });
	}
	/* 获取数据信息 */
	getData();
	function getData(){
		$.ajax({
			url:'<%=basePath%>Resource/getData',
			type:'GET',
			dataType:'JSON',
			success:function(data){
				if (data.code=='200'){
					$(data.data).each(function(index,ele){
							$('#name'+ele.id).val(ele.type);
							$('#data'+ele.id).val(ele.count);
							$('#text'+ele.id).val(ele.content);
					});
				}
			},
			error:function(){
				toastr.error('网络异常加载数据失败','失败');
			}
		});
	}
	/* 保存数据信息  */
	function save(obj){
		new $.flavr({
			content : '确认提交后立即生效',
            dialog : 'confirm', 
            onConfirm : function(){ 
            	var no = $(obj).attr('data');
        		var name = $('#name'+no).val();
        		var data = $('#data'+no).val();
        		var text = $('#text'+no).val();
        		if (!name){
        			toastr.warning('标题字段不能为空', '操作失败');
        			return false;
        		}
        		if (!data){
        			toastr.warning('数据不能为空', '操作失败');
        			return false;
        		}
        		if (!text){
        			toastr.warning('介绍不能为空', '操作失败');
        			return false;
        		}
        		$.ajax({
        			url:'<%=basePath%>Resource/saveData',
        			type : 'POST',
        			dataType : 'JSON',
        			data : {
        				'id' : no,
        				'data' : data,
        				'name':name,
        				'text':text
        			},
        			success : function(data) {
        				if (data.code == '200') {
        					toastr.success('提交成功', '操作成功');
        				} else {
        					toastr.warning(data.message, '操作失败');
        				}
        			},
        			error : function() {
        				toastr.error('服务器发烧了>_<,请稍后再试', '网络异常');
        			}
        		});
            }, 
            onCancel : function(){
            	toastr.warning('取消操作','取消操作');
           	 } 
         });
	}
	/* 获取公司信息 */
	getCompanys()
	function getCompanys(){
		$.ajax({
			url:'<%=basePath%>Resource/getCompanys',
			type:'GET',
			dataType:'JSON',
			success:function(data){
				var html = '';
				if (data.code=='200'){
					$(data.data).each(function(index,ele){
						type = '战略企业';
						if (ele.type==2){
							type = '合作案例';
						}
						html = html + '<tr><td>'+ele.id+'</td><td>'+ele.companyName+'</td><td><img style="height:30px;with:30px" src="'+ele.logo+'"/></td><td>'+type+'</td>'+
						'<td><button data-id="'+ele.id+'" data-name="'+ele.companyName+'" data-info="'+ele.info+'" data-website="'+ele.webSite+'" data-logo="'+ele.logo+'"  onclick="showCompany(this)" class="btm">查 看</button><button data="'+ele.id+'" onclick="delCompany(this)" class="btm">删 除</button></td></tr>'
					});
				}
				$('#companysTableBody').html(html);
			},
			error:function(){
				toastr.error('网络异常加载数据失败','失败');
			}
		});
	}
	
	function delCompany(obj){
		new $.flavr({
			content : '确认删除？',
            dialog : 'confirm', 
            onConfirm : function(){ 
            	var id = $(obj).attr('data');
            	var tr = $(obj).parent().parent();
        		$.ajax({
        			url:'<%=basePath%>Resource/delCompany',
        			type : 'POST',
        			dataType : 'JSON',
        			data : {
        				'id' : id,
        			},
        			success : function(data) {
        				if (data.code == '200') {
        					toastr.success('提交成功', '操作成功');
        					$(tr).fadeOut(300);
        				} else {
        					toastr.warning(data.message, '操作失败');
        				}
        			},
        			error : function() {
        				toastr.error('服务器发烧了>_<,请稍后再试', '网络异常');
        			}
        		});
            }, 
            onCancel : function(){
            	toastr.warning('取消操作','取消操作');
           	 } 
         });
	}
	
	/* 显示选中的公司信息 */
	function showCompany(obj){
		var src = $(obj).attr('data-logo');
		var info = $(obj).attr('data-info');
		var name = $(obj).attr('data-name');
		var webSite = $(obj).attr('data-website');
		var id = $(obj).attr('data-id');
		$('#showCompanyImg').attr('src',src);
		$('#companyUrl').val(webSite);
		$('#companyShowName').val(name);
		$('#companyShowInfo').val(info);
		$('#editCompanyInfoBtn').attr('data',id);
	}
	
	function saveCompanyInfo(obj){
		var url = $('#companyUrl').val();
		var name = $('#companyShowName').val();
		var info = $('#companyShowInfo').val();
		var type = $(obj).attr('data-type');
		var code = $('#typeCode').val();
		var id = 0;
		var formData = new FormData();
		$('#saveCompanyInfoBtn').attr('disabled',true);
		$('#editCompanyInfoBtn').attr('disabled',true);
		if (type=='1'){
			if (!document.getElementById("companyShowLogo").files[0]){
				toastr.error('请选择文件','失败');
				$('#saveCompanyInfoBtn').attr('disabled',false);
				$('#editCompanyInfoBtn').attr('disabled',false);
				return false;
			}
			formData.append('file',document.getElementById("companyShowLogo").files[0]);
			formData.append('type',code);
		}
		if (type=='2'){
			id = $(obj).attr('data');
			formData.append('type',0);
		}
		if (!url){
			toastr.error('企业网址不能为空','失败');
			$('#saveCompanyInfoBtn').attr('disabled',false);
			$('#editCompanyInfoBtn').attr('disabled',false);
			return false;
		}
		if (!name){
			toastr.error('企业名称不能为空','失败');
			$('#saveCompanyInfoBtn').attr('disabled',false);
			$('#editCompanyInfoBtn').attr('disabled',false);
			return false;
		}
		if (!info){
			toastr.error('企业介绍不能为空','失败');
			$('#saveCompanyInfoBtn').attr('disabled',false);
			$('#editCompanyInfoBtn').attr('disabled',false);
			return false;
		}
		
		formData.append('companyName',name);
		formData.append('webSite',url);
		formData.append('info',info);
		formData.append('id',id);
		formData.append('file',document.getElementById("companyShowLogo").files[0]);
		$.ajax({
			url:'<%=basePath%>/Resource/saveOrUpdateCompany',
			type:'POST',
			data:formData,
			contentType: false,
			processData: false,
			success:function(data){
				if (data.code=='200'){
					toastr.success('上传成功','成功');
					setTimeout(function(){
						window.location.reload();
					}, 800);
				} else{
					toastr.error(data.message,'失败');
					$('#saveCompanyInfoBtn').attr('disabled',false);
					$('#editCompanyInfoBtn').attr('disabled',false);
				}
			},
			error:function(){
				toastr.error('网络异常','失败');
				$('#saveCompanyInfoBtn').attr('disabled',false);
				$('#editCompanyInfoBtn').attr('disabled',false);
			}
		});
	}
</script>